package com.jxzq.schoolwork.sysadmin.service.impl;

import com.jxzq.schoolwork.sysadmin.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Created by shaopengxun on 2017/11/9.
 *
 * @version 1.0
 * @description Excel表格中数据读取demo
 * @author: shaopengxun
 * @createTime: 2017/11/9
 */
@Service
public class ExcelDemo {
   
    public void  testDemo() throws IOException {
        // 编写解析代码逻辑
        // 基于.xls 格式解析 HSSF
        // 1、 加载Excel文件对象
        File file = new File("C:\\Users\\shaopengxun\\Desktop\\test.xls");
        String fileName = file.getName();
        if (!fileName.matches("^.*\\.(?i)(xls|xlsx)$")){
            System.out.println("文件类型不正确!");
        }
        int i = fileName.lastIndexOf(".");
        String substring = fileName.substring(i + 1);
        System.out.println(substring);
        if ("xls".equals(substring)) {
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
            // 2、 读取一个sheet
            Sheet sheet = hssfWorkbook.getSheetAt(0);
            // 3、 读取sheet中每一行
            getCells(sheet);
        } else if ("xlsx".equals(substring)) {
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file));
            // 2、 读取一个sheet
            Sheet sheet = xssfWorkbook.getSheetAt(0);
            // 3、 读取sheet中每一行
            getCells(sheet);
        }
    }

    private void getCells(Sheet sheet) {
        for (Row row : sheet) {
            // 一行数据 对应 一个区域对象
            if (row.getRowNum() == 0) {
                // 第一行 跳过
                continue;
            }
            // 跳过空行
            if (row.getCell(0) == null
                    || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
                continue;
            }
            System.out.println(getValue(row.getCell(0)));
            System.out.println(getValue(row.getCell(1)));
        }
    }

    //解决excel类型问题，获得数值
    public  String getValue(Cell cell) {
        String value = "";
        if(null==cell){
            return value;
        }
        switch (cell.getCellType()) {
            //数值型
            case Cell.CELL_TYPE_NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    //如果是date类型则 ，获取该cell的date值
                    Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    value = format.format(date);;
                }else {// 纯数字
                    BigDecimal big=new BigDecimal(cell.getNumericCellValue());
                    value = big.toString();
                    //解决1234.0  去掉后面的.0
                    if(null!=value&&!"".equals(value.trim())){
                        String[] item = value.split("[.]");
                        if(1<item.length&&"0".equals(item[1])){
                            value=item[0];
                        }
                    }
                }
                break;
            //字符串类型
            case Cell.CELL_TYPE_STRING:
                value = cell.getStringCellValue().toString();
                break;
            // 公式类型
            case Cell.CELL_TYPE_FORMULA:
                //读公式计算值
                value = String.valueOf(cell.getNumericCellValue());
                if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
                    value = cell.getStringCellValue().toString();
                }
                break;
            // 布尔类型
            case Cell.CELL_TYPE_BOOLEAN:
                value = " "+ cell.getBooleanCellValue();
                break;
            // 空值
            case Cell.CELL_TYPE_BLANK:
                value = "";
                System.out.println("excel出现空值");
                break;
            // 故障
            case Cell.CELL_TYPE_ERROR:
                value = "";
                System.out.println("excel出现故障");
                break;
            default:
                value = cell.getStringCellValue().toString();
        }
        if("null".endsWith(value.trim())){
            value="";
        }
        return value;
    }
    public static void main(String[] args) throws IOException {
        ExcelDemo excelService = new ExcelDemo();
        excelService.testDemo();
    }
}